package com.ddw.weewar;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ddw.weewar.api.WeewarApi;
import com.ddw.weewar.api.WeewarUser;

public class LoginActivity extends Activity implements View.OnClickListener {

    private Button mLoginButton;
    private EditText mUsernameEditText;
    private EditText mApiKeyEditText;
    private TextView mCreateAccountTextView;
    private TextView mFindApiKeyTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        // Get widgets
        mLoginButton = (Button) findViewById(R.id.login_button);
        mUsernameEditText = (EditText) findViewById(R.id.username_edit_text);
        mApiKeyEditText = (EditText) findViewById(R.id.api_key_edit_text);
        mCreateAccountTextView = (TextView) findViewById(R.id.create_account_text_view);
        mFindApiKeyTextView = (TextView) findViewById(R.id.find_api_key_text_view);

        // Setup event handlers
        this.mLoginButton.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.login, menu);
        return true;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login_button:
                doLogin();
                break;
            case R.id.create_account_text_view:
                doCreateAccount();
                break;
            case R.id.find_api_key_text_view:
                doFindApiKey();
                break;
        }
    }

    /**
     * Performs a login using the UI values
     */
    private void doLogin() {
        // Get username and check its valid
        String username = mUsernameEditText.getText().toString();
        if(username == null || username.trim().length() == 0) {
            Toast.makeText(this, R.string.username_invalid, Toast.LENGTH_SHORT).show();
            return;
        }

        // Set the button to disabled
        mLoginButton.setText(R.string.login_attempt);
        mLoginButton.setEnabled(false);

        // Make the request
        WeewarApi.user(username, new WeewarApi.UserCallback() {

            @Override
            public void onSuccess(WeewarUser user) {
                Toast.makeText(getApplicationContext(), "Points: " + user.getPoints() + "\nGames Running: " + user.getGamesRunning() + "\nID: " + user.getId(), Toast.LENGTH_LONG).show();

                // Re-enable the button
                mLoginButton.setText(R.string.login);
                mLoginButton.setEnabled(true);
            }

            @Override
            public void onFailure(Throwable exception) {
                Toast.makeText(getApplicationContext(), "Failed to obtain user info: " + exception.getMessage(), Toast.LENGTH_LONG).show();

                // Re-enable the button
                mLoginButton.setText(R.string.login);
                mLoginButton.setEnabled(true);
            }

        });
    }

    /**
     * Opens the users web browser to create a new account
     */
    private void doCreateAccount() {
    }

    /**
     * Opens the users web browser to find their api key
     */
    private void doFindApiKey() {
    }

}
